package kn.pulldata.mq;

/**
 * 类描述：用户id缓存
 *
 * @author XJW
 * @since 2025/7/8 15:38
 */
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SimpleUserCache {

    // 缓存对象
    private static final Map<Integer, CacheEntry> USER_CACHE = new ConcurrentHashMap<>();

    // 缓存有效时长（10分钟）
    private static final long EXPIRE_MILLIS = 10 * 60 * 1000;

    public static List<Integer> get(Integer orgaId) {
        CacheEntry entry = USER_CACHE.get(orgaId);
        if (entry != null && (Instant.now().toEpochMilli() - entry.timestamp) < EXPIRE_MILLIS) {
            return entry.data;
        }
        USER_CACHE.remove(orgaId); // 过期了，移除
        return null;
    }

    public static void put(Integer orgaId, List<Integer> data) {
        USER_CACHE.put(orgaId, new CacheEntry(data));
    }

    // 缓存实体（数据 + 时间戳）
    private static class CacheEntry {
        long timestamp;
        List<Integer> data;

        CacheEntry(List<Integer> data) {
            this.data = data;
            this.timestamp = Instant.now().toEpochMilli();
        }
    }
}

